﻿using BLL;
using Common;
using IBLL;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApp1.学生
{
    public partial class StudentInterface : Form
    {
        IFrQuestionBankManage_IBLL frQuestion = new FrQuestionBankManage_BLL();
        IFrExamManage_IBLL frExam = new FrExamManage_BLL();
        public StudentInterface()
        {
            InitializeComponent();
            uiDataGridView1.DataSource = frExam.SubMit_info("select 编号=PapersNumber,名称=PapersName,开始时间=TestStartTime,持续时间=TestDuration,考试地点=ClassName,课目=CourseName from view_2").Tables[0];
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (uiDataGridView1.SelectedRows.Count !=1)
            {
                return;
            }
            DateTime dateTime = Convert.ToDateTime(uiDataGridView1.SelectedRows[0].Cells[2].Value);
            TimeSpan t3 =DAL.SQLHepler.GetDateTime() .Subtract(dateTime);
            double getHours = t3.TotalMinutes; //将这个天数转换成小时, 返回值是double类型的  
                                               //   Console.WriteLine(getHours);
            if (getHours > Convert.ToDouble(uiDataGridView1.SelectedRows[0].Cells[3].Value))
            {

                MessageBox.Show("本次考试已结束");
                return;
            }

            SqlDataReader reader = frQuestion.Query($"select StudentId from Examinee_Score where Exam_id={uiDataGridView1.SelectedRows[0].Cells[0].Value} and StudentId={login_Com.account.Account} ;");
            if (reader.Read())
            {
                MessageBox.Show("本次考试已提交");
                return;
            };
            Form2.time = dateTime.AddMinutes(Convert.ToDouble( uiDataGridView1.SelectedRows[0].Cells[3].Value));
            Form2  form2 = new Form2(uiDataGridView1.SelectedRows[0].Cells[0].Value.ToString());
            form2.ShowDialog();
        }

        private void uiButton1_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void uiDataGridView1_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            if (uiDataGridView1.SelectedRows.Count < 0 || e.RowIndex < 0)
            {
                return;
            }

            uiDataGridView1.Rows[e.RowIndex].Selected = true;
        }

        private void uiDataGridView1_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            if (uiDataGridView1.SelectedRows.Count < 0 || e.RowIndex < 0)
            {
                return;
            }

            uiDataGridView1.Rows[e.RowIndex].Selected = true;
        }

        private void StudentInterface_Load(object sender, EventArgs e)
        {

        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (uiDataGridView1.SelectedRows.Count != 1)
            {
                return;
            }
            SqlDataReader reader= frExam.Check($"select 总分=(SingleChoice+MultipleChoice+TrueOrFalseQuestions+Completion) from Examinee_Score where Exam_id='{uiDataGridView1.SelectedRows[0].Cells[0].Value}' and StudentId='{login_Com.account.Account}';");
            if (reader.Read())
            {
                MessageBox.Show($"本次考试成绩为{reader[0]}");
            }
            else
            {
                MessageBox.Show($"未找到本次考试成绩");
            }
            reader.Close();
        }
    }
}
